﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using RoadTeam.DAL;
using System.Data;
using System.Reflection;
using RoadTeam.Enums;
using RoadTeam.Model;
using RoadTeam.BLL;
using System.Reflection;
using RoadTeam.Standard;

public partial class 反射_Table2Object : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DictBLL bll = new DictBLL();
        DataTable dt = bll.GetDictSync("RmBuildingKind");
        Dict dict = new Dict();
        List<Dict> li = ConverDtToList.DtToList<Dict>(dt);
    }
    //public object Tran(DataTable dt, Type type)
    //{
    //    object o= Activator.CreateInstance(type);
    //    Assembly _Assembly = Assembly.Load("mscorlib");
    //    Type _TypeList = _Assembly.GetType("System.Collections.Generic.List`1[[" + type.FullName + "," + type.Assembly.FullName + "]]");
    //    object _List =System.Activator.CreateInstance(_TypeList);
    //    Convert.ChangeType(_List, type);
    //    return _List;
    //}

}

public class ConverDtToList
{
    public static List<T> DtToList<T>(DataTable dt)
    {
        List<T> li = new List<T>();
        T entity = default(T);
        String TempName = String.Empty;
        foreach (DataRow dr in dt.Rows)
        {
            entity = Activator.CreateInstance<T>();
            PropertyInfo[] PropertInfos = entity.GetType().GetProperties();

            foreach (PropertyInfo p in PropertInfos)
            {
                TempName = p.Name;
                if (!dt.Columns.Contains(TempName)) continue;
                if (!p.CanWrite) continue;

                object obj = dr[TempName];
                if (obj == null || obj == DBNull.Value) continue;
                p.SetValue(entity, obj, null);
            }
            li.Add(entity);
        }
        return li;
    }
}

